package com.luo.chengrui.module.sa.controller.admin.datapermission;

import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionConfigViewDO;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionConfigExcludeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionConfigDO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionConfigConvert;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionConfigService;

@Tag(name = "管理后台 - 数据权限配置")
@RestController
@RequestMapping("/sa/data-permission-config")
@Validated
public class DataPermissionConfigController {

    @Resource
    private DataPermissionConfigService dataPermissionConfigService;

    @Resource
    private DataPermissionConfigExcludeService dataPermissionConfigExcludeService;

    @PostMapping("/create")
    @Operation(summary = "创建数据权限配置")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:create')")
    public CommonResult<String> createDataPermissionConfig(@Valid @RequestBody DataPermissionConfigCreateReqVO createReqVO) {
        return success(dataPermissionConfigService.createDataPermissionConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据权限配置")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:update')")
    public CommonResult<Boolean> updateDataPermissionConfig(@Valid @RequestBody DataPermissionConfigUpdateReqVO updateReqVO) {
        dataPermissionConfigService.updateDataPermissionConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据权限配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:delete')")
    public CommonResult<Boolean> deleteDataPermissionConfig(@RequestParam("id") String id) {
        dataPermissionConfigService.deleteDataPermissionConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据权限配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:query')")
    public CommonResult<DataPermissionConfigRespVO> getDataPermissionConfig(@RequestParam("id") String id) {
        DataPermissionConfigDO dataPermissionConfig = dataPermissionConfigService.getDataPermissionConfig(id);
        return success(DataPermissionConfigConvert.INSTANCE.convert(dataPermissionConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据权限配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:query')")
    public CommonResult<List<DataPermissionConfigRespVO>> getDataPermissionConfigList(@RequestParam("ids") Collection<String> ids) {
        List<DataPermissionConfigDO> list = dataPermissionConfigService.getDataPermissionConfigList(ids);
        return success(DataPermissionConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据权限配置分页")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:query')")
    public CommonResult<PageResult<DataPermissionConfigRespVO>> getDataPermissionConfigPage(@Valid DataPermissionConfigPageReqVO pageVO) {
//        PageResult<DataPermissionConfigDO> pageResult = dataPermissionConfigService.getDataPermissionConfigPage(pageVO);
        PageResult<DataPermissionConfigViewDO> pageResult = dataPermissionConfigService.getDataPermissionConfigPageCustomize(pageVO);
        return success(DataPermissionConfigConvert.INSTANCE.convertPage2(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据权限配置 Excel")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:export')")
    @OperateLog(type = EXPORT)
    public void exportDataPermissionConfigExcel(@Valid DataPermissionConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataPermissionConfigDO> list = dataPermissionConfigService.getDataPermissionConfigList(exportReqVO);
        // 导出 Excel
        List<DataPermissionConfigExcelVO> datas = DataPermissionConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据权限配置.xls", "数据", DataPermissionConfigExcelVO.class, datas);
    }


    @PostMapping("/exclude/create")
    @Operation(summary = "创建数据权限继承排除表 记录从上级机构继承并排除权限配置项。")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:update')")
    public CommonResult<String> createDataPermissionConfigExclude(@Valid @RequestBody DataPermissionConfigExcludeCreateReqVO createReqVO) {
        return success(dataPermissionConfigExcludeService.createDataPermissionConfigExclude(createReqVO));
    }


    @DeleteMapping("/exclude/delete")
    @Operation(summary = "删除数据权限继承排除表 记录从上级机构继承并排除权限配置项。")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-config:update')")
    public CommonResult<Boolean> deleteDataPermissionConfigExclude(@RequestParam("id") String id) {
        dataPermissionConfigExcludeService.deleteDataPermissionConfigExclude(id);
        return success(true);
    }

}
